import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import codecs
import datetime
import Core.MongoDB as MongoDB
import Core.Label as Label
import Core.Gadget as Gadget
import math
import pymongo
import copy
import random
from Factors.General import *
from Factors.Price import *
from Factors.Profitability import *
from Factors.Growth import *
from Factors.Valuation import *
from Factors.MarketStyle import *
from Factors.CapitalAllocation import *
from Factors.CashFlow import *
from Factors.Volatility import *
from Factors.Misc import *
# from sklearn.linear_model import LinearRegression

from Core.DataView import DataView
database = DataView.BatchView()


def UpdateInstrumentFactors(datetime1, datetime2):
    stocks = database.find("Instruments", "Stock")
    indexs = database.find("Instruments", "Index")
    testInstruments = database.find("Instruments", "Stock", query={"limit":400})
    testInstruments = database.find("Instruments", "Stock", query={"Symbol": "000001.SZ"})
    totalInstrument = stocks + indexs
    hs300 = database.find("Instruments", "Index", query={"Symbol": "000300.SH"})
    shanghai = database.find("Instruments", "Index", query={"Symbol": "000001.SH"})

    # ---Price---
    CalcMonthlyReturn(database, totalInstrument, datetime1, datetime2, update=False)
    # CalcReturn(database, totalInstrument, datetime1, datetime2, update=True)

    # ---Market Model / Alpha Beta Volatility ---
    #CalcCAPModel(database, stocks, datetime1, datetime2, update=False)
    #CalcVolatilityFactor(database, totalInstrument, datetime1, datetime2, update=False)

    # ---Profitability---
    # CalcProfitabilityFactor(database, stocks, datetime1, datetime2, update=True)

    # ---Valuation---
    CalcValuatioFactor(database, totalInstrument, datetime1, datetime2, update=True)

    # ---Historical Growth---
    CalcGrowthFactor(database, stocks, datetime1, datetime2, update=True)

    # ---CashFlow---
    #CalcCashFlowFactor(database, stocks, datetime1, datetime2, update=False)

    # ---Capital Allocation---

    # ---Operating Flag---

    # ---Liquidity---

    # ---Share Holder---

    # ---Market Style Base on Cap / PB(Valuation)---
    #CalcMarketStyle(database, instruments, datetime1, datetime2)


def CalcInstrumentFactors(instruments, datetime1, datetime2):

    # ---Price---
    CalcMonthlyReturn(database, instruments, datetime1, datetime2)
    CalcReturn(database, instruments, datetime1, datetime2)

    # CalcMomentum(database, instruments, datetime1, datetime2)

    # ---Market Model / Alpha Beta Volatility IdioValotility---
    CalcCAPModel(database, instruments, datetime1, datetime2)

    # ---Valuation & Size---
    CalcValuatioFactor(database, instruments, datetime1, datetime2)

    # ---Market Style Base on Cap / PB(Valuation)---
    # CalcMarketStyle(database, instruments, datetime1, datetime2)

    # ---Profitability---
    # DeleteFactor(database,selectedInstrument,"OperatingProfitZScore")
    # DeleteFactor(database,selectedInstrument,"OperatingProfitRanking")
    # DeleteFactor(database,selectedInstrument,"OperatingProfit")
    # DeleteFactor(database, "NetIncome")
    CalcProfitabilityFactor(database, instruments, datetime1, datetime2)
    # CalcProfitabilityRanking(database, selectedInstrument, datetime1, datetime2)
    PlotFactor("000022.SZ","OperatingProfitZScore")
    # PlotFactor("000022.SZ","OperatingProfitRanking")

    # ---Growth---
    # DeleteFactor(database,instruments,"Growth_CAGR_OperatingPrifit_Year1")
    # DeleteFactor(database,instruments,"Growth_CAGR_OperatingPrifit_Year2")
    # DeleteFactor(database,instruments,"Growth_CAGR_OperatingPrifit_Year3")
    # DeleteFactor(database,instruments,"Growth_CAGR_NetIncome_Year1")
    # DeleteFactor(database,instruments,"Growth_CAGR_NetIncome_Year2")
    # DeleteFactor(database,instruments,"Growth_CAGR_NetIncome_Year3")
    # DeleteFactor(database,instruments,"Growth_OperatingPrifit_YoY")
    # DeleteFactor(database,instruments,"Growth_NetIncome_YoY")
    # database.creatIndex("Factor", "Growth_CAGR_NetIncome", "StdDateTime")
    # database.creatIndex("Factor", "Growth_CAGR_NetIncome", "Symbol")
    # database.creatIndex("Factor", "Growth_CAGR_OperatingProfit", "StdDateTime")
    # database.creatIndex("Factor", "Growth_CAGR_OperatingProfit", "Symbol")
    CalcGrowthFactor(database,instruments,datetime1,datetime2)

    # ---CashFlow---

    # ---Capital Allocation---

    # ---Operating Flag---

    # ---Liquidity---

    # ---Share Holder---


def CalcProfileFactors(instrments, datetime1, datetime2):
    print("CalcProfileFactors")


def CalcLabels(instrments, datetime1, datetime2):
    print("CalcLabels")

# database = MongoDB.MongoDB("10.13.38.31", "27017")
#---证券列表---
filter = {}
#filter = {"Symbol": "000001.SZ"}
#filter["limit"] = 100
instruments = database.findWithFilter("Instruments", "Stock", filter)


#
datetime1 = datetime.datetime(2018, 8, 30)
datetime2 = datetime.datetime(2018, 10, 24)
datetime1 = Gadget.ToUTCDateTime(datetime1)
datetime2 = Gadget.ToUTCDateTime(datetime2)

#df = IO.LoadFactorsAsDataFrame(database,"000001.SZ",["MonthlyReturn"],datetime1,datetime2)
#df.to_csv("d:/monthlyreturn.csv")
#print(df.head())
#CalcInstrumentFactors(instruments, datetime1, datetime2)
UpdateInstrumentFactors(datetime1,datetime2)

#---
#FindMostSuitableSeasonalReportAlignDate(database)


#industries = Industries(instruments)
#BuildFactorDatabase(database,instruments)

#selectedInstrument = industries["交通运输"]


#PlotFactor(database, "002530.SZ","BookToMarket")
#df = LoadSingleFactor(database, "BookToMarket", datetime3)
#df.to_csv("D://Data//DIYFinancialAdvisor//" + "test.csv")
pass